package com.xnyzc.lhy.order.controller.capi.carpool;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.order.entity.param.carpool.InviteCarpoolParam;
import com.xnyzc.lhy.order.service.carpool.InviteCarpoolService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @Description 邀约拼车*
 * @Author zhuyubin
 * @Date 2019-09-25 18:01
 **/
@RestController
@RequestMapping(value = "/capi/inviteCarpool")
public class InviteCarpoolController {

    @Autowired
    private InviteCarpoolService inviteCarpoolService;

    /**
     * 修改邀约拼车订单
     * @param inviteCarpoolParam 入参
     * @return
     */
    @PostMapping("/updateInviteCarpoolOrder")
    public Rv updateInviteCarpoolOrder(
            @ApiParam(name = "inviteCarpoolParam", value = "订单号、乘客手机号、乘客姓名、出发地信息、目的地信息")
            @RequestBody InviteCarpoolParam inviteCarpoolParam){
        Long userId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        // 乘车人姓名
        String inviteName = inviteCarpoolParam.getInviteName();
        // 乘车人姓名
        String invitePhone = inviteCarpoolParam.getInvitePhone();
        // 目的地地址、经度、纬度
        String endAddress = inviteCarpoolParam.getEndAddress();
        String endLng = inviteCarpoolParam.getEndLng();
        String endLat = inviteCarpoolParam.getEndLat();
        // 出发地地址、经度、纬度
        String startAddress = inviteCarpoolParam.getStartAddress();
        String startLng = inviteCarpoolParam.getStartLng();
        String startLat = inviteCarpoolParam.getStartLat();
        if (CheckUtil.objIsEmpty(inviteName, invitePhone, endAddress, endLng, endLat, startAddress, startLng, startLat)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(EErrorCode.success, inviteCarpoolService.updateInviteCarpoolOrder(userId, tag, inviteCarpoolParam), new HashMap<>(16));
    }

    /**
     * 根据手机号显示选项
     * @param inviteCarpoolParam 入参
     * @return
     */
    @PostMapping(value = "/inviteCarpoolInfo")
    public Rv inviteCarpoolInfo(@ApiParam(name = "inviteCarpoolParam", value = "手机号") @RequestBody InviteCarpoolParam inviteCarpoolParam){
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        String invitePhone = inviteCarpoolParam.getInvitePhone();
        if (CheckUtil.objIsEmpty(invitePhone)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(inviteCarpoolService.inviteCarpoolInfo(invitePhone, tag));
    }
}
